﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using thinger.VisionModels;

namespace thinger.VisitionDAL
{
    public class IniConfigHelper
    {
        public static string IniPath = "";

        #region API函数声明

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        //需要调用GetPrivateProfileString的重载
        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        private static extern uint GetPrivateProfileStringA(string section, string key,
            string def, byte[] retVal, int size, string filePath);

        #endregion

        public static List<string> ReadSections(string iniFilename)
        {
            List<string> result = new List<string>();
            byte[] buf = new byte[65536];
            uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, iniFilename);
            int j = 0;
            for (int i = 0; i < len; i++)
                if (buf[i] == 0)
                {
                    result.Add(Encoding.Default.GetString(buf, j, i - j));
                    j = i + 1;
                }
            return result;
        }

        public static List<string> ReadKeys(string SectionName, string iniFilename)
        {
            List<string> result = new List<string>();
            byte[] buf = new byte[65536];
            uint len = GetPrivateProfileStringA(SectionName, null, null, buf, buf.Length, iniFilename);
            int j = 0;
            for (int i = 0; i < len; i++)
                if (buf[i] == 0)
                {
                    result.Add(Encoding.Default.GetString(buf, j, i - j));
                    j = i + 1;
                }
            return result;
        }

        #region 读Ini文件

        public static string ReadIniData(string Section, string Key, string NoText)
        {
            return ReadIniData(Section, Key, NoText, IniPath);
        }
        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(2048);
                GetPrivateProfileString(Section, Key, NoText, temp, 2048, iniFilePath);
                return temp.ToString();
            }
            else return string.Empty;
        }

        #endregion

        #region 写Ini文件

        public static bool WriteIniData(string Section, string Key, string Value)
        {
            return WriteIniData(Section, Key, Value, IniPath);
        }

        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
            if (OpStation == 0)
                return false;
            else return true;
        }
        #endregion


        #region 读取配置文件和解析端口配置
       public static (SysConfig, SerialPortSet) LoadSysConfig(String configPath)
        {
            SerialPortSet serialPortSet = new SerialPortSet();
            //string info = IniConfigHelper.ReadIniData("系统配置", "系统配置", "", Application.StartupPath + "\\config" + "\\config.ini");
            string boardInfo = IniConfigHelper.ReadIniData("系统配置", "系统配置", "", configPath);
            serialPortSet.Port = IniConfigHelper.ReadIniData("螺丝机参数", "串口号", "", configPath);
            serialPortSet.BaudRate = IniConfigHelper.ReadIniData("螺丝机参数", "波特率", "", configPath);
            serialPortSet.Parity = IniConfigHelper.ReadIniData("螺丝机参数", "校验位", "", configPath);
            serialPortSet.DataBits = IniConfigHelper.ReadIniData("螺丝机参数", "数据位", "", configPath);
            serialPortSet.StopBits = IniConfigHelper.ReadIniData("螺丝机参数", "停止位", "", configPath);
            serialPortSet.LScrewAddress = IniConfigHelper.ReadIniData("螺丝机参数", "左螺丝刀地址", "", configPath);
            serialPortSet.RScrewAddress = IniConfigHelper.ReadIniData("螺丝机参数", "右螺丝刀地址", "", configPath);
            serialPortSet.DataFormat = IniConfigHelper.ReadIniData("螺丝机参数", "大小端", "", configPath);
            serialPortSet.LScrewTorque = IniConfigHelper.ReadIniData("螺丝机参数", "左螺丝刀扭矩", "", configPath);
            serialPortSet.RScrewTorque = IniConfigHelper.ReadIniData("螺丝机参数", "右螺丝刀扭矩", "", configPath);
            //DataFormat
            serialPortSet.IniPath = configPath;
            if (boardInfo.Length > 0)
            {
                return (JsonHelper.JSONToEntity<SysConfig>(boardInfo), serialPortSet);
            }
            else
            {
                return (null, serialPortSet);
            }
        }

        #endregion
    }
}
